package ihm;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;

import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextField;

import network.Connexion;

import data.Authentification;
import data.Indice;
import data.ListeIndices;
import data.ModeleListeAlertes;
import data.ModeleListeIndices;

public class DemanderIndice extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	//private ModeleListeIndices modele;
	private  Connexion c ;
	

	/**
	 * Launch the application.
	 */
	public DemanderIndice(final Connexion con,final ListeIndices listIndices, final Authentification auth) throws SQLException {
		setTitle("Murder Party - demander un ");		
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			}
		});
		
		//modele = mdoui;
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 259, 288);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblDemanderUnIndice = new JLabel("Demander un indice");
		lblDemanderUnIndice.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
		lblDemanderUnIndice.setBounds(50, 11, 152, 14);
		contentPane.add(lblDemanderUnIndice);
		
		JLabel lblIndice = new JLabel("Indice  :");
		lblIndice.setBounds(26, 72, 46, 14);
		contentPane.add(lblIndice);
		
		JLabel lblCout = new JLabel("Cout : ");
		lblCout.setBounds(26, 121, 46, 14);
		contentPane.add(lblCout);
		
		final JComboBox comboBox = new JComboBox();
		comboBox.setBounds(73, 69, 113, 20);
		contentPane.add(comboBox);
		
		int i;

		comboBox.addItem(" ");
		for ( i = 0 ; i < listIndices.size();i++){
		
				
			if ( listIndices.get(i).isPossedeIndice() == false )
			comboBox.addItem(listIndices.get(i).getLabelIndice());
				
			}
		
	
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int j ;
				String nouvelleString = (String) comboBox.getSelectedItem();
				for ( j = 0 ; j < listIndices.size();j++){
					
					if ( listIndices.get(j).getLabelIndice().equals(nouvelleString) ){
						int cout =listIndices.get(j).getCout_indice();
						System.out.println(cout);
						textField.setText(String.valueOf(cout));
						
					}
			}
			}
			});
		
		/* ajout de tous les indices de la base
		
		
		 */
		JButton btnValider = new JButton("Valider");
		btnValider.setBounds(26, 199, 65, 23);
		contentPane.add(btnValider);
		btnValider.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
					int j ;
					String nouvelleString = (String) comboBox.getSelectedItem();
					for ( j = 0 ; j < listIndices.size();j++){
						
						if ( listIndices.get(j).getLabelIndice().equals(nouvelleString) ){
							
							con.send(listIndices.get(j));
							System.out.println("[Client][DemanderIndice] envoi indice");
							con.send(auth.getUtilisateur());
						}
				}
					FermerFenetre();
			}
			});
		
		JButton btnAnnuler = new JButton("Annuler");
		btnAnnuler.setBounds(113, 199, 89, 23);
		contentPane.add(btnAnnuler);
		btnAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			
				FermerFenetre();
			}
			});
		
		textField = new JTextField();
		textField.setEditable(false);
		textField.setBounds(60, 118, 32, 20);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel lblMins = new JLabel("mins");
		lblMins.setBounds(102, 121, 46, 14);
		contentPane.add(lblMins);
	}

	protected void demander_indice( int id) {
		
	}

	private void FermerFenetre() {
		this.dispose();
			
		}
}
